﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace test01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button_01_Click(object sender, EventArgs e)
        {
            string test = "hello world!";
            this.textBox_test01.Width = 150;
            this.textBox_test01.Height = 150;
            this.textBox_test01.Text = test;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.listView1.Items.Clear(); //先清空listview1中所有数据
            OpenFileDialog oFD1 = new OpenFileDialog();
            oFD1.InitialDirectory = "F:\\";   //显示初始目录，
            oFD1.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*";   //文件名筛选字符串
            oFD1.FilterIndex = 2;
            oFD1.RestoreDirectory = true;
            //若用户在对话框中单击确定，则结果为DialogResult.OK
            //否则结果为DialogResult.Cancel

            if (oFD1.ShowDialog() == DialogResult.OK)       //打开文件
            {
                MessageBox.Show("成功打开文件：" + oFD1.FileName);    //显示选择的文件名
                FileStream fs = new FileStream(oFD1.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);  //需添加头文件system.IO
                StreamReader s1 = new StreamReader(fs, Encoding.ASCII);

                string sepatator = ",";  //以逗号分割字符串
                char[] cgap = sepatator.ToCharArray();

                string[][] data = new string[100][];
                for (int i = 0; i < 999; i++)
                {
                    string str1 = s1.ReadLine();   //读取一行字符
                    if (str1 == null) break;
                    string[] str2 = str1.Split(cgap, StringSplitOptions.None); //基于数组中的字符将字符串拆分为多个子字符串
                    ListViewItem b = new ListViewItem();
                    b.SubItems[0].Text = (i + 1).ToString();
                    b.SubItems.Add(str2[0]);
                    b.SubItems.Add("");
                    b.SubItems.Add("");
                    b.SubItems.Add("");
                    b.SubItems.Add(str2[1]);
                    b.SubItems.Add(str2[2]);
                    b.SubItems.Add(str2[3]);
                    listView1.Items.Add(b);
                }
            }
        }
    }
}
